#include "CF_Includes.h"

CCF_Window::CCF_Window(HINSTANCE instance)
{
	CCF_Settings* pSettings = CCF_Settings::getClassSettings();
	this->m_instance = instance;
	this->m_title = L"CrossFire";
	this->m_x = 0;
	this->m_y = 0;
	this->m_width = pSettings->getWidth();
	this->m_height = pSettings->getHeight();
}

CCF_Window::~CCF_Window(void)
{
}
BOOL CCF_Window::RegisterWindowClass()
{
  BOOL result;
  WNDCLASSEXW wndClass;

  if( !GetClassInfoExW( m_instance, CROSS_FIRE_WINDOW_CLASS, &wndClass ))
  {
    ZeroMemory(&wndClass, sizeof( wndClass ));

    wndClass.cbSize = sizeof( wndClass );
    wndClass.lpszClassName = CROSS_FIRE_WINDOW_CLASS;
    wndClass.lpfnWndProc = WindowProc;
    wndClass.style = CS_CLASSDC;

    result = (RegisterClassExW( &wndClass ) != 0);
  }

  return result;
}

LRESULT CALLBACK CCF_Window::WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  switch( msg )
  {
  case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
  }

  return DefWindowProc(hwnd, msg, wParam, lParam);
}

BOOL CCF_Window::Initialize()
{
  BOOL result = true;

  if((result = this->RegisterWindowClass()))
  {
    m_hwnd = CreateWindowExW( NULL, CROSS_FIRE_WINDOW_CLASS, m_title, WS_OVERLAPPEDWINDOW,
      m_x, m_y, m_width, m_height, NULL, NULL, m_instance, NULL );
  }
  else
  {
    MessageBoxW( NULL, L"Can't register window class", L"Error", MB_OK );
  }

  return result;
}

HWND CCF_Window::GetHandle()
{
  return this->m_hwnd;
}

void CCF_Window::Show()
{
  ShowWindow( this->m_hwnd, SW_SHOW );
}

BOOL CCF_Window::IsDestroy()
{
  MSG msg; 

  if(PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
  {
    TranslateMessage( &msg );
    DispatchMessage( &msg );
  }

  return msg.message == WM_QUIT;
}